<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ConfigCookie.php 323 2008-12-01 18:32:53Z steen.rabol@gmail.com $
 *
 */

class ConfigCookie extends PortalAdminPage
{
	const CONFIG_FILE = 'Application.Data.Settings';

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Cookie Configuration');

		if($this->IsPostBack)
		{
			return;
		}

		$parameters = $this->Application->Parameters;

		// Cookie information
		$this->UseCookieExpire->Checked	= TPropertyValue::ensureBoolean($parameters['UseCookieExpire']);
		$this->CookiePrefix->Text		= $parameters['CookiePrefix'];
		$this->LoginExpire->Text		= $parameters['LoginExpire'];
	}

	public function saveButtonClicked($sender, $param)
	{
		$dom = new TXmlDocument;

		if($dom->loadFromFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml')))
		{
			if(!$this->UpdateParameter($dom,'UseCookieExpire', $this->UseCookieExpire->Checked))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('UseCookieExpire', $this->UseCookieExpire->Checked);
			}

			if(!$this->UpdateParameter($dom,'CookiePrefix',$this->CookiePrefix->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('CookiePrefix', $this->CookiePrefix->Text);
			}

			if(!$this->UpdateParameter($dom,'LoginExpire',$this->LoginExpire->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('LoginExpire', $this->LoginExpire->Text);
			}

			$dom->saveToFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml'));
		}

		$this->gotoDefaultPage();
	}

	private function createParameter($id, $value)
	{
		$element = new TXmlElement('parameter');
		$element->Attributes['id'] = $id;
		$element->Attributes['value'] = TPropertyValue::ensureString($value);
		return $element;
	}

	public function UpdateParameter(&$dom,$id,$value)
	{
		$parameters = &$dom->getElementsByTagName('parameter');
		foreach ($parameters as $param)
		{
			if($id == $param->getAttribute("id"))
			{
				$param->setAttribute('value',$value);
				return true;
			}
		}

		return false;
	}
}
?>